﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.BitTorrent;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using Jayden.Dll.Core.IO;

namespace Jayden.Console.BitTorrent
{
	class Program
	{
		static byte[] peerId = new byte[20];
		static Random random = new Random();

		static void Main(string[] args)
		{
			string fileName = @"C:\Users\Jonathan\Downloads\Game.of.Thrones.S03E01.REPACK.720p.HDTV.x264-EVOLVE.[eztv].torrent";

			try
			{
				BitTorrentFile bitTorrentFile = new BitTorrentFile(fileName);
				random.NextBytes(peerId);
				BitTorrentFileManager manager = new BitTorrentFileManager(peerId, bitTorrentFile);
				if (bitTorrentFile.Announce.StartsWith("udp://"))
				{
					Uri uri = new Uri(bitTorrentFile.Announce);
					BitTorrentTrackerUdpClient udpClient = new BitTorrentTrackerUdpClient(manager, uri);
					udpClient.BitTorrentPeerReceived += new BitTorrentPeerReceived(manager.OnBitTorrentPeerReceived);
					udpClient.Connect();
				}
			}
			catch(Exception e)
			{
				System.Console.WriteLine(e);
			}

			System.Console.WriteLine("Press any key to exit program.");
			System.Console.ReadKey(true);
		}
	}
}
